package com.pdwl.service.impl;

import com.pdwl.common.dtos.ResponseResult;
import com.pdwl.common.enums.AppHttpCodeEnum;
import com.pdwl.common.vo.AreaSimpleVo;
import com.pdwl.mapper.AreaMapper;
import com.pdwl.pojo.Area;
import com.pdwl.service.AreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: TLB
 * @Date: 2023/1/12 17:49
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 根据父级行政id查询简单地区信息
     * @param parentId
     * @return
     */
    @Override
    public ResponseResult<List<AreaSimpleVo>> listSimple(Integer parentId) {

        if (parentId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        List<Area> areas = areaMapper.selectByParentId(parentId);

        List<AreaSimpleVo> areaSimpleVos = areas.stream().map(
                area -> {
                    AreaSimpleVo areaSimpleVo = new AreaSimpleVo();
                    BeanUtils.copyProperties(area,areaSimpleVo);
                    return areaSimpleVo;
                }
        ).collect(Collectors.toList());
        return ResponseResult.okResult(areaSimpleVos);
    }
}
